//
//  ASAddResearchProjectVC.m
//  AttendenceSys
//
//  Created by 窦芳俊 on 16/5/18.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ASAddResearchProjectVC.h"
#import "FTPhotoAlbumController.h"
#import "FTAlbumPhotosController.h"


@interface ASAddResearchProjectVC ()

@end

@implementation ASAddResearchProjectVC

- (instancetype)initWithTypeId:(NSInteger)typeId
                  researchType:(NSString *)rType
                     rTypeInfo:(ASResearchTypeInfo*)typeInfo
{
    self = [super init];
    if (self)
    {
        _typeID = typeId;
        _resTypeName = rType;
        _rTypeInfo = typeInfo;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _releaseArrImage = [[NSMutableArray alloc] init];
    
    self.strNavTitle = _resTypeName;
    [self setRightText:@"保存"];
    
    [self initProjectUI];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)initProjectUI
{
    /////////    项目
    //    项目名称
    //    立项单位
    //    立项时间
    //    结项时间（未结项，或者具体时间）
    //    项目级别：院级、校级、省级、国家级
    //    成员排名：第一成员、第二成员、第三。。。。第八
    //    项目描述
    
    CGFloat padding = 10.0f;
    // 项目名称
    _researchA = [[UILabel alloc] initWithFrame:CGRectMake(padding, 20, 70, 20)];
    _researchA.font = [UIFont systemFontOfSize:16];
    _researchA.text = @"项目名称:";
    [_researchA setTextAlignment:NSTextAlignmentLeft];
    _researchA.textColor = [UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0];
    [self.view addSubview:_researchA];
    
    _inputresearchA = [[UITextField alloc] initWithFrame:CGRectMake(_researchA.frame.origin.x + _researchA.frame.size.width + 5,
                                                                    _researchA.frame.origin.y,
                                                                    self.view.frame.size.width - 80 - 10,
                                                                    20)];
    _inputresearchA.clearButtonMode = UITextFieldViewModeWhileEditing;
    _inputresearchA.textColor = [UIColor blackColor];
    _inputresearchA.returnKeyType = UIReturnKeyDone;
    _inputresearchA.placeholder = @"输入项目名称";
    [_inputresearchA setBackgroundColor:[UIColor clearColor]];
    _inputresearchA.delegate = self;
    _inputresearchA.font = [UIFont systemFontOfSize:16.0f];
    _inputresearchA.borderStyle = UITextBorderStyleNone;
    [self.view addSubview:_inputresearchA];
    
    // 立项单位
    _researchB = [[UILabel alloc] initWithFrame:CGRectZero];
    _researchB.frame = CGRectMake(padding, _researchA.frame.origin.y + _researchA.frame.size.height + 18, 70, 20);
    _researchB.font = [UIFont systemFontOfSize:16];
    _researchB.text = @"立项单位:";
    [_researchB setTextAlignment:NSTextAlignmentLeft];
    _researchB.textColor = [UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0];
    [self.view addSubview:_researchB];
    
    _inputresearchB = [[UITextField alloc] initWithFrame:CGRectMake(_researchB.frame.origin.x + _researchB.frame.size.width + 5,
                                                                    _researchB.frame.origin.y,
                                                                    self.view.frame.size.width - 80 - 10,
                                                                    20)];
    _inputresearchB.clearButtonMode = UITextFieldViewModeWhileEditing;
    _inputresearchB.textColor = [UIColor blackColor];
    _inputresearchB.returnKeyType = UIReturnKeyDone;
    _inputresearchB.placeholder = @"输入立项单位";
    [_inputresearchB setBackgroundColor:[UIColor clearColor]];
    _inputresearchB.delegate = self;
    _inputresearchB.font = [UIFont systemFontOfSize:16.0f];
    _inputresearchB.borderStyle = UITextBorderStyleNone;
    [self.view addSubview:_inputresearchB];
    
    // 立项时间
    _researchC = [[UILabel alloc] initWithFrame:CGRectZero];
    _researchC.frame = CGRectMake(padding, _researchB.frame.origin.y + _researchB.frame.size.height + 18, 70, 20);
    _researchC.font = [UIFont systemFontOfSize:16];
    _researchC.text = @"立项时间:";
    [_researchC setTextAlignment:NSTextAlignmentLeft];
    _researchC.textColor = [UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0];
    [self.view addSubview:_researchC];
    
    _inputresearchC = [[UITextField alloc] initWithFrame:CGRectMake(_researchC.frame.origin.x + _researchC.frame.size.width + 5,
                                                                    _researchC.frame.origin.y,
                                                                    self.view.frame.size.width - 80 - 10,
                                                                    20)];
    _inputresearchC.clearButtonMode = UITextFieldViewModeWhileEditing;
    _inputresearchC.textColor = [UIColor blackColor];
    _inputresearchC.returnKeyType = UIReturnKeyDone;
    _inputresearchC.placeholder = @"输入立项时间";
    [_inputresearchC setBackgroundColor:[UIColor clearColor]];
    _inputresearchC.delegate = self;
    _inputresearchC.font = [UIFont systemFontOfSize:16.0f];
    _inputresearchC.borderStyle = UITextBorderStyleNone;
    [self.view addSubview:_inputresearchC];
    
    // 结项时间
    _researchD = [[UILabel alloc] initWithFrame:CGRectZero];
    _researchD.frame = CGRectMake(padding, _researchC.frame.origin.y + _researchC.frame.size.height + 18, 70, 20);
    _researchD.font = [UIFont systemFontOfSize:16];
    _researchD.text = @"结项时间:";
    [_researchD setTextAlignment:NSTextAlignmentLeft];
    _researchD.textColor = [UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0];
    [self.view addSubview:_researchD];
    
    _inputresearchD = [[UITextField alloc] initWithFrame:CGRectMake(_researchD.frame.origin.x + _researchD.frame.size.width + 5,
                                                                    _researchD.frame.origin.y,
                                                                    self.view.frame.size.width - 80 - 10,
                                                                    20)];
    _inputresearchD.clearButtonMode = UITextFieldViewModeWhileEditing;
    _inputresearchD.textColor = [UIColor blackColor];
    _inputresearchD.returnKeyType = UIReturnKeyDone;
    _inputresearchD.placeholder = @"输入结项时间";
    [_inputresearchD setBackgroundColor:[UIColor clearColor]];
    _inputresearchD.delegate = self;
    _inputresearchD.font = [UIFont systemFontOfSize:16.0f];
    _inputresearchD.borderStyle = UITextBorderStyleNone;
    [self.view addSubview:_inputresearchD];
    
    // 项目级别
    _researchE = [[UILabel alloc] initWithFrame:CGRectZero];
    _researchE.frame = CGRectMake(padding, _researchD.frame.origin.y + _researchD.frame.size.height + 18, 70, 20);
    _researchE.font = [UIFont systemFontOfSize:16];
    _researchE.text = @"项目级别:";
    [_researchE setTextAlignment:NSTextAlignmentLeft];
    _researchE.textColor = [UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0];
    [self.view addSubview:_researchE];
    
    _inputresearchE = [[UITextField alloc] initWithFrame:CGRectMake(_researchE.frame.origin.x + _researchE.frame.size.width + 5,
                                                                    _researchE.frame.origin.y,
                                                                    self.view.frame.size.width - 80 - 10,
                                                                    20)];
    _inputresearchE.clearButtonMode = UITextFieldViewModeWhileEditing;
    _inputresearchE.textColor = [UIColor blackColor];
    _inputresearchE.returnKeyType = UIReturnKeyDone;
    _inputresearchE.placeholder = @"输入项目级别";
    [_inputresearchE setBackgroundColor:[UIColor clearColor]];
    _inputresearchE.delegate = self;
    _inputresearchE.font = [UIFont systemFontOfSize:16.0f];
    _inputresearchE.borderStyle = UITextBorderStyleNone;
    [self.view addSubview:_inputresearchE];
    
    // 成员排名
    _researchF = [[UILabel alloc] initWithFrame:CGRectZero];
    _researchF.frame = CGRectMake(padding, _researchE.frame.origin.y + _researchE.frame.size.height + 18, 70, 20);
    _researchF.font = [UIFont systemFontOfSize:16];
    _researchF.text = @"成员排名:";
    [_researchF setTextAlignment:NSTextAlignmentLeft];
    _researchF.textColor = [UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0];
    [self.view addSubview:_researchF];
    
    _inputresearchF = [[UITextField alloc] initWithFrame:CGRectMake(_researchF.frame.origin.x + _researchF.frame.size.width + 5,
                                                                    _researchF.frame.origin.y,
                                                                    self.view.frame.size.width - 80 - 10,
                                                                    20)];
    _inputresearchF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _inputresearchF.textColor = [UIColor blackColor];
    _inputresearchF.returnKeyType = UIReturnKeyDone;
    _inputresearchF.placeholder = @"输入成员排名";
    [_inputresearchF setBackgroundColor:[UIColor clearColor]];
    _inputresearchF.delegate = self;
    _inputresearchF.font = [UIFont systemFontOfSize:16.0f];
    _inputresearchF.borderStyle = UITextBorderStyleNone;
    [self.view addSubview:_inputresearchF];
    
    // 项目描述
    _researchG = [[UILabel alloc] initWithFrame:CGRectZero];
    _researchG.frame = CGRectMake(padding, _researchF.frame.origin.y + _researchF.frame.size.height + 18, 70, 20);
    _researchG.font = [UIFont systemFontOfSize:16];
    _researchG.text = @"项目描述:";
    [_researchG setTextAlignment:NSTextAlignmentLeft];
    _researchG.textColor = [UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0];
    [self.view addSubview:_researchG];
    
    _inputresearchG = [[UITextField alloc] initWithFrame:CGRectMake(_researchG.frame.origin.x + _researchG.frame.size.width + 5,
                                                                    _researchG.frame.origin.y,
                                                                    self.view.frame.size.width - 80 - 10,
                                                                    20)];
    _inputresearchG.clearButtonMode = UITextFieldViewModeWhileEditing;
    _inputresearchG.textColor = [UIColor blackColor];
    _inputresearchG.returnKeyType = UIReturnKeyDone;
    _inputresearchG.placeholder = @"输入项目描述";
    [_inputresearchG setBackgroundColor:[UIColor clearColor]];
    _inputresearchG.delegate = self;
    _inputresearchG.font = [UIFont systemFontOfSize:16.0f];
    _inputresearchG.borderStyle = UITextBorderStyleNone;
    [self.view addSubview:_inputresearchG];
    
    [self initScrollView:_releaseArrImage];
}

// 删除背景视图
- (void)removeBackView
{
    if (_backgroundViewBegin)
    {
        [_backgroundViewBegin removeFromSuperview];
        _backgroundViewBegin = nil;
    }
    
    if (_backgroundViewEnd)
    {
        [_backgroundViewEnd removeFromSuperview];
        _backgroundViewEnd = nil;
    }
}

- (UIView*)topView
{
    UIViewController *recentView = self;
    
    while (recentView.parentViewController != nil)
    {
        recentView = recentView.parentViewController;
    }
    
    return recentView.view;
}

- (void)initScrollView:(NSMutableArray*)arrImage
{
    if (_imageScrollView)
    {
        [_imageScrollView removeFromSuperview];
    }
    
    _imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    _imageScrollView.frame = CGRectMake(0,
                                        _researchG.frame.origin.y + _researchG.frame.size.height + 130,
                                        [UIScreen mainScreen].bounds.size.width,
                                        70);
    _imageScrollView.directionalLockEnabled = YES; //只能一个方向滑动
    _imageScrollView.pagingEnabled = YES; //是否翻页
    _imageScrollView.backgroundColor = [UIColor clearColor];
    _imageScrollView.showsVerticalScrollIndicator = NO; //垂直方向的滚动指示
    _imageScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    _imageScrollView.showsHorizontalScrollIndicator = YES;//水平方向的滚动指示
    _imageScrollView.userInteractionEnabled = YES;
    _imageScrollView.delegate = self;
    [self.view addSubview:_imageScrollView];
    
    //设置滚动视图的位置
    [_imageScrollView setContentSize:CGSizeMake(([UIScreen mainScreen].bounds.size.width/5*(arrImage.count+1)),
                                                _imageScrollView.frame.size.height)];
    
    for (int i = 0; i < arrImage.count; i++)
    {
        NSData* data = [arrImage objectAtIndex:i];
        UIButton* imageBtnView = [[UIButton alloc] initWithFrame:CGRectMake(10*(i+1) + i*([UIScreen mainScreen].bounds.size.width-60)/5,
                                                                            0,
                                                                            ([UIScreen mainScreen].bounds.size.width-60)/5,
                                                                            ([UIScreen mainScreen].bounds.size.width-60)/5)];
        [imageBtnView setBackgroundColor:[UIColor redColor]];
        imageBtnView.contentMode = UIViewContentModeScaleAspectFill;
        //UIViewContentModeScaleAspectFit
        UIImage* image = [UIImage imageWithData:data];
        [imageBtnView setBackgroundImage:image forState:UIControlStateNormal];
        [_imageScrollView addSubview:imageBtnView];
    }
    
    NSInteger count = arrImage.count;
    UIButton* addBtn = [[UIButton alloc] initWithFrame:CGRectMake(10*(count+1) + count*([UIScreen mainScreen].bounds.size.width-60)/5,
                                                                  0,
                                                                  ([UIScreen mainScreen].bounds.size.width-60)/5,
                                                                  ([UIScreen mainScreen].bounds.size.width-60)/5)];
    addBtn.backgroundColor = [UIColor clearColor];
    [addBtn setBackgroundImage:[UIImage imageNamed:@"ic_add"] forState:UIControlStateNormal];
    [_imageScrollView addSubview:addBtn];
    [addBtn addTarget:self action:@selector(addImage) forControlEvents:UIControlEventTouchUpInside];
    [_imageScrollView addSubview:addBtn];
}

- (void)addImage
{
    FTPhotoAlbumController* phoAlbum = [[FTPhotoAlbumController alloc] init];
    phoAlbum.albumDelegate = self;
    phoAlbum.videoDelegate = self;
    phoAlbum.albumType = AlbumType_MutiForSending;
    ASBaseNavigationController* baseNavi = [[ASBaseNavigationController alloc] initWithRootViewController:phoAlbum];
    [self presentViewController:baseNavi animated:YES completion:nil];
}

- (void)popToListVC
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightButtonPressed:(id)action
{
    [self uploadSelectImages];
}

// 上传选择的图片
- (void)uploadSelectImages
{
    // 上传图片接口 http://211.65.216.54/uploadAction.do?type=1&userCode=xiaohu
    // type=1 上传头像  2表示请假  3表示 发布成长   其他表示发布活动
    
    NSString* refer = @"uploadAction.do?type=3&userCode=";
    NSString* prefix = kASHttpUrl(refer);
    NSString* userCode = [ASAppDelegate sharedAppDelegate].userInfo.userCode;
    NSString* totalUrl = [prefix stringByAppendingString:userCode];
    
    // 提交信息上传
    NSString* achiA = _inputresearchA.text;     //项目名称
    NSString* achiB = _inputresearchB.text;     //立项单位
    NSString* achiC = _inputresearchC.text;     //立项时间
    NSString* achiD = _inputresearchD.text;     //结项时间
    NSString* achiE = _inputresearchE.text;     //项目级别
    NSString* achiF = _inputresearchF.text;     //成员排名
    NSString* achiG = _inputresearchG.text;     //项目描述
    
    NSString* infoUrl = [NSString stringWithFormat:@"&attr1=%@&condtionTypeId=%ld&attr2=%@&attr3=%@&attr4=%@&attr5=%@&attr6=%@&attr7=%@&researchSubjectId=%@",
                         achiA,
                         (long)_typeID,
                         achiB,
                         achiC,
                         achiD,
                         achiE,
                         achiF,
                         achiG,
                         _rTypeInfo.researchSubjectId];
    
    totalUrl = [totalUrl stringByAppendingString:infoUrl];
    
    [SVProgressHUD showWithStatus:@"上传图片..." maskType:SVProgressHUDMaskTypeClear];
    
    typeof(self) __weak weakSelf = self;
    ASNetworkEngine *netEngine = [ASNetworkEngine sharedInstance];
    [netEngine uploadImagesWithUrl:totalUrl image:_releaseArrImage progressBlock:^(long long bytesRead, long long totalBytes) {
        
    } completionBlock:^(id responseObject) {
        
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        if (isSuccessRsp(jsonRsp))
        {
            [SVProgressHUD showSuccessWithStatus:@"新增成功"];
            
            [weakSelf popToListVC];
        }
        
    } failure:^(NSString *errorMsg) {
        
        [SVProgressHUD showErrorWithStatus:errorMsg.description];
    }];
}

#pragma mark - ASResearchLevelVCDelegate
- (void)selectResearchLevel:(NSString *)level
{
    _inputresearchE.text = level;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - ASParticipantBankingVCDelegate
- (void)selectParticipantBanking:(NSString *)pBanking
{
    _inputresearchF.text = pBanking;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - FTAlbumPhotoViewControllerDelegate

- (void)albumDidFinishPickingWithInfo:(NSMutableArray *)info
                          IsOriginImg:(BOOL)isOriginImg
                        withAlbumType:(AlbumType)albumType
{
    _releaseArrImage = info;
    [self initScrollView:_releaseArrImage];
    
    NSInteger offsetCount = 0;
    if (4 < _releaseArrImage.count)
    {
        offsetCount = _releaseArrImage.count - 4;
        [_imageScrollView setContentOffset:CGPointMake(10*offsetCount + offsetCount*([UIScreen mainScreen].bounds.size.width-60)/5, 0)];
    }
}

#pragma mark - RCSVideoPickerDelegate
- (void)selectedVideoDictionary:(NSDictionary *)videoDic
                 pickController:(UIViewController *)ctr
                      videoData:(NSMutableArray*)videoData
{
}

#pragma mark - ASDatePickerViewDelegate

- (void)datePickerCancel
{
    [self removeBackView];
}

- (void)dataPickSelect:(NSInteger)selectTag selectDate:(NSString*)selectData
{
    NSString *strDate = selectData;
    NSRange range = [strDate rangeOfString:@" "];
    if (range.length > 0)
    {
        strDate = [strDate substringToIndex:range.location];
    }
    
    if (selectTag == 0)
    {
        _inputresearchC.text = strDate;
    }
    else
    {
        _inputresearchD.text = strDate;
    }
    
    [self removeBackView];
}

- (void)initDatePickerViewBegin
{
    _dateSelctPickerViewBegin = [[ASDatePickerView alloc] initWithFrame:CGRectMake(0,
                                                                                   self.view.frame.size.height- 216 - 46,
                                                                                   [UIScreen mainScreen].bounds.size.width,
                                                                                   216 + 46)];
    _dateSelctPickerViewBegin.iTag = 0;
    _dateSelctPickerViewBegin.editTimeLab.text = @"选择日期";
    _dateSelctPickerViewBegin.delegate = self;
    _dateSelctPickerViewBegin.datePickerView.datePickerMode = UIDatePickerModeDate;
    //NSDate* minDate = [NSDate date];
    //_dateSelctPickerViewBegin.datePickerView.minimumDate = minDate;
    
    [self.view addSubview:_dateSelctPickerViewBegin];
}

- (void)initDatePickerViewEnd
{
    _dateSelctPickerViewEnd = [[ASDatePickerView alloc] initWithFrame:CGRectMake(0,
                                                                                 self.view.frame.size.height- 216 - 46,
                                                                                 [UIScreen mainScreen].bounds.size.width,
                                                                                 216 + 46)];
    _dateSelctPickerViewEnd.iTag = 1;
    _dateSelctPickerViewEnd.editTimeLab.text = @"选择日期";
    _dateSelctPickerViewEnd.delegate = self;
    _dateSelctPickerViewEnd.datePickerView.datePickerMode = UIDatePickerModeDate;
    //NSDate* minDate = [NSDate date];
    //_dateSelctPickerViewEnd.datePickerView.minimumDate = minDate;
    
    [self.view addSubview:_dateSelctPickerViewEnd];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    [textField resignFirstResponder];
    
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (_inputresearchC == textField)
    {
        UIView* sourceView = [self topView];
        _backgroundViewBegin = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                                        0,
                                                                        sourceView.frame.size.width,
                                                                        sourceView.frame.size.height - 216 - 46)];
        [_backgroundViewBegin setBackgroundColor:[UIColor blackColor]];
        _backgroundViewBegin.alpha = 0.5;
        [sourceView addSubview:_backgroundViewBegin];
        [self initDatePickerViewBegin];
        
        return NO;
    }
    
    if (_inputresearchD == textField)
    {
        UIView* sourceView = [self topView];
        _backgroundViewEnd = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                                      0,
                                                                      sourceView.frame.size.width,
                                                                      sourceView.frame.size.height - 216 - 46)];
        [_backgroundViewEnd setBackgroundColor:[UIColor blackColor]];
        _backgroundViewEnd.alpha = 0.5;
        [sourceView addSubview:_backgroundViewEnd];
        [self initDatePickerViewEnd];
        return NO;
    }
    
    if (_inputresearchE == textField)
    {
        ASResearchLevelVC *lVC = [[ASResearchLevelVC alloc] init];
        lVC.hidesBottomBarWhenPushed = YES;
        lVC.delegate = self;
        [self.navigationController pushViewController:lVC animated:YES];
        
        return NO;
    }
    
    if (_inputresearchF == textField)
    {
        // 作者排名
        ASParticipantBankingVC *pVC = [[ASParticipantBankingVC alloc] init];
        pVC.hidesBottomBarWhenPushed = YES;
        pVC.delegate = self;
        [self.navigationController pushViewController:pVC animated:YES];
        
        return NO;
    }
    
    return YES;
}



@end
